<template>
	<v-container class="views-app-version-list">
		<v-data-table :mobile-breakpoint="0" mobile-breakpointclass="elevation-1" :items="data.list" :options="data.options" @update:options="data.update($event)" :headers="headers" :server-items-length="data.total" :loading="data.loading" hide-default-footer disable-sort :hide-default-header="!$size.sm">
			<template v-slot:top>
				<v-toolbar flat color="white">
					<v-toolbar-title>{{name}}</v-toolbar-title>
					<v-spacer></v-spacer>
					<v-btn @click="onEdit()" color="primary">新版本</v-btn>
					<v-btn @click="data.search(0)" color="success" class="ml-2">刷新</v-btn>
				</v-toolbar>
			</template>
			<template v-slot:item.tools="{item}">
				<v-btn small @click="onEdit(item)" color="primary" text>修改</v-btn>
				<v-btn small @click="del(item)" color="red" text>删除</v-btn>
			</template>
			<template v-slot:footer v-if="data.totalPage>1">
				<v-divider></v-divider>
				<i-page class="pa-2" circle :value="data.options.page-1" @input="data.options.page=$event+1" :total="data.total"></i-page>
			</template>
		</v-data-table>
		<v-btn v-if="!$size.sm" block text>加载更多</v-btn>
	</v-container>
</template>
<script>
export default {
	name: "AppVersionList",
	props: {
		name: String,
	},
	data() {
		return {
			data: new utils.DataSource(this.query),
			body: {
				version: '',
				description: '',
			},
			add_dialog: false,
		}
	},
	computed: {
		headers() {
			return [{
				text: '#',
				value: 'id',
			}, {
				text: '版本号',
				value: 'version',
				sortable: false,
			}, {
				text: '介绍',
				value: 'description',
				sortable: false,
			}, {
				text: '操作',
				value: 'tools',
				sortable: false,
			}]
		}
	},
	methods: {
		query(query) {
			query.sortBy = 'id'
			query.desc = 1;
			return this.$get('app/version_list', query)
		},
		async onEdit(item) {
			let body = await ivue.form({
				value: item,
				params: {
					"version": {
						"lbl": "版本号",
						"len": [0, 16],
						"need": "id"
					},
					"description": {
						"lbl": "版本信息",
						"len": [0, 256]
					},
					"url": {
						"lbl": "下载链接",
						"len": [0, 256],
						"def": "https://quan2go.inu1255.cn/app"
					}
				}
			})
			let data = await this.$post('app/version_add', body);
			if (item) {
				Object.assign(item, body);
				ivue.toast.success('修改成功');
			} else {
				this.data.search()
				ivue.toast.success('发布成功');
			}
		},
		async del(item) {
			if (await ivue.confirm('你确定要删除吗?').then(x => false, () => true)) return;
			await this.$get('app/version_del', { id: item.id })
			this.data.search()
		},
	},
	mounted() {

	},
	components: {

	},
}
</script>
